<?php

class homepageComponents extends sfComponents {

    /**
     * Main Header
     * 
     * @param sfWebRequest $request
     */
    public function executeHeader(sfWebRequest $request) {
        
    }

    /**
     * Цэс
     * 
     * @param sfWebRequest $request
     */
    public function executeMenu(sfWebRequest $request) {
        $menu = menuHelper::getMenu();

        $attrs = array('class' => 'page-sidebar-menu');
        $active = $this->getContext()->getRouting()->getCurrentRouteName();
        $this->menu = menuHelper::factory($menu)->render($attrs, $active);
    }

    /**
     * Хуудас болгоны дээр гарах header
     * 
     * @param sfWebRequest $request
     */
    public function executeContentHeader(sfWebRequest $request) {
        $activeRoute = $this->getContext()->getRouting()->getCurrentRouteName();

        # as edit & update goes to same uri
        $activeRoute = str_replace('_edit', '_add', $activeRoute);

        $menu = menuHelper::getMenu();
        $path = menuHelper::getPath($activeRoute, $menu);

        $this->path = $path;
    }

    public function executeDbSalesMonthly(sfWebRequest $request) {
        $dateBegin = date('Y-m-01', strtotime("-10 months"));
        $dateEnd = date('Y-m-t');

        $rows = ReportCore::getSalesGroupedByMonths($dateBegin, $dateEnd);

        $arr = array('label' => 'Сүүлийн 10 сарын орлого', 'data' => array());
        foreach ($rows as $row) {
            $arr['data'][] = array(strtotime($row['year'] . '-' . $row['month'] . '-01') * 1000, (double) $row['total']);
        }

        $this->data = json_encode($arr);
    }

}
